﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace AGameOfThrones.board
{
    class region : ContentControl
    {
        private WriteableBitmap m_WriteableBitmap;

        public String m_RegionName;
        public int m_RegionType;
        public int m_RegionID;
        public int m_iCenterX;  
        public int m_iCenterY;

        public int m_iMinX;
        public int m_iMinY;
        public int m_iMaxX;
        public int m_iMaxY;
        Pen pen = new Pen();

        public region()
        {
            Highlight = false;

        }

        private bool _highlight;
        public bool Highlight
        {
            get
            {
                return _highlight;
            }
            set
            {
                if (_highlight != value)
                {
                    _highlight = value;
                    this.InvalidateVisual();
                    
                }
            }
        }

        public Panel Panel
        {
            get { return this.Content as Panel; }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            if(Highlight)
            {
                Rect rect = new Rect(m_iMinX, m_iMinY, m_WriteableBitmap.PixelWidth, m_WriteableBitmap.PixelHeight);
                drawingContext.DrawImage(m_WriteableBitmap, rect);
            }

            //drawingContext.DrawRectangle(Brushes.Black, pen, new Rect(0, 0, 600, 400));
            //drawingContext.DrawEllipse(Brushes.Green, pen, new Point(300, 300), 50, 50); 
        }

        public void InitHighlight(byte[] in_GrayScaleImage, int in_Width, int in_Height)
        {
            m_iMinX = in_Width;
            m_iMinY = in_Height;

            for (int y = 0; y < in_Height; y++)
            {
                for (int x = 0; x < in_Width; x++)
                {
                    int index = (y * in_Width) + x;
                    int value = in_GrayScaleImage[index];

                    if (value == m_RegionID)
                    {
                        m_iMinX = Math.Min(x, m_iMinX);
                        m_iMinY = Math.Min(y, m_iMinY);
                        m_iMaxX = Math.Max(x, m_iMaxX);
                        m_iMaxY = Math.Max(y, m_iMaxY);
                    }

                }
            }

            m_WriteableBitmap = new WriteableBitmap(m_iMaxX - m_iMinX, m_iMaxY - m_iMinY, 96, 96, PixelFormats.Bgra32, null);

            uint[] pixels = new uint[m_WriteableBitmap.PixelWidth * m_WriteableBitmap.PixelHeight];
            for (int y = m_iMinY; y < m_iMaxY; y++)
            {
                for (int x = m_iMinX; x < m_iMaxX; x++)
                {
                    int index = (y * in_Width) + x;
                    int value = in_GrayScaleImage[index];
 
                    int column = x - m_iMinX;
                    int row = y - m_iMinY;
                    int dest = (row * m_WriteableBitmap.PixelWidth) + column;
                    try
                    {
                        if (value == m_RegionID)
                        {
                            pixels[dest] = 0x3f00ffff;
                        }
                        else
                        {
                            pixels[dest] = 0x00000000;
                        } 
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine(ex);
                    }                
                }
            }

            Int32Rect rect = new Int32Rect( 0, 0, m_WriteableBitmap.PixelWidth, m_WriteableBitmap.PixelHeight);
            m_WriteableBitmap.WritePixels(rect, pixels, m_WriteableBitmap.PixelWidth*4, 0);


            Canvas p = new Canvas();
            p.Width = m_WriteableBitmap.PixelWidth;
            p.Height = m_WriteableBitmap.PixelHeight;             
            p.Margin = new Thickness(m_iMinX, m_iMinY, 0, 0);

            try
            {
                p.Name = m_RegionName.Replace(" ", String.Empty).Replace("'", String.Empty);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
            }
            
            this.Content = p;

        }

    }
}
